package com.dingding.common.core.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingding.common.core.util.picturerecognition.Client;
import com.dingding.common.core.util.picturerecognition.Request;
import com.dingding.common.core.util.picturerecognition.Response;
import com.dingding.common.core.util.picturerecognition.constant.Constants;
import com.dingding.common.core.util.picturerecognition.constant.ContentType;
import com.dingding.common.core.util.picturerecognition.constant.HttpHeader;
import com.dingding.common.core.util.picturerecognition.constant.HttpSchema;
import com.dingding.common.core.util.picturerecognition.dto.JSZContentDTO;
import com.dingding.common.core.util.picturerecognition.enums.Method;
import com.dingding.common.core.util.picturerecognition.util.MessageDigestUtil;

/**
 * 
 * @ClassName: PictureRecognitionUtil 
 * @Description: 图片(驾驶证、行驶证)识别
 * @author YHQ
 * @date 2016年10月17日 上午9:46:53
 * 
 */
public class PictureRecognitionUtil {
	
    //APP KEY
    private final static String APP_KEY = "23482004";
    // APP密钥
    private final static String APP_SECRET = "c2f5b3f3db7c0185c339cf7e900845dd";
    //API域名
    private final static String HOST = "dm-53.data.aliyun.com";
    //自定义参与签名Header前缀（可选,默认只有"X-Ca-"开头的参与到Header签名）
    private final static List<String> CUSTOM_HEADERS_TO_SIGN_PREFIX = new ArrayList<String>();
	
	
	public static JSZContentDTO jszRecognition(String str) throws Exception{
		
        //请求path
        String path = "/rest/160601/ocr/ocr_vehicle.json";

        String body = "{\"inputs\": [{\"image\": {\"dataType\": 50,\"dataValue\": \""+str+"\"}}]}";
        
        Map<String, String> headers = new HashMap<String, String>();
        //（必填）根据期望的Response内容类型设置
        headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "application/json");
        //（可选）Body MD5,服务端会校验Body内容是否被篡改,建议Body非Form表单时添加此Header
        headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(body));
        //（POST/PUT请求必选）请求Body内容格式
        headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);

        Request request = new Request(Method.POST_STRING, HttpSchema.HTTPS + HOST, path, APP_KEY, APP_SECRET, Constants.DEFAULT_TIMEOUT);
        request.setHeaders(headers);
        request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
        
        request.setStringBody(body);

        //调用服务端
        Response response = Client.execute(request);

        String aaa = response.getBody().replaceAll( "\\s", "" );
        
        JSONObject jsonObject = JSONUtil.parseObject(response.getBody()).getJSONArray("outputs").getJSONObject(0).getJSONObject("outputValue");
        
        System.out.println(jsonObject);
        
        String bodyStr = aaa.substring(aaa.indexOf("\"dataValue\":") + 13, aaa.lastIndexOf("}") - 6);
        System.out.println(bodyStr.replaceAll("\\\"", "\""));
//        JSZContentDTO jszContentDTO = JSONUtil.parseObject(bodyStr,JSZContentDTO.class);
        JSZContentDTO jszContentDTO = null;
        
		return null;
	}

}
